﻿using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.PeteBrown;

namespace NesGamepadTest
{
    public partial class Program
    {
        NesGamepadModule gamepad = new NesGamepadModule(9);


        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            /*******************************************************************************************
            Modules added in the Program.gadgeteer designer view are used by typing 
            their name followed by a period, e.g.  button.  or  camera.
            
            Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
                button.ButtonPressed +=<tab><tab>
            
            If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
                GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
                timer.Tick +=<tab><tab>
                timer.Start();
            *******************************************************************************************/


            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");


            //PollingTest();
            EventTest();
        }


        private void EventTest()
        {
            gamepad.ButtonAPressed += new NesGamepadModule.ButtonEventHandler(gamepad_ButtonAPressed);
            gamepad.ButtonAReleased += new NesGamepadModule.ButtonEventHandler(gamepad_ButtonAReleased);

            gamepad.ButtonBPressed += new NesGamepadModule.ButtonEventHandler(gamepad_ButtonBPressed);
            gamepad.ButtonBReleased += new NesGamepadModule.ButtonEventHandler(gamepad_ButtonBReleased);

            gamepad.SelectPressed += new NesGamepadModule.ButtonEventHandler(gamepad_SelectPressed);
            gamepad.SelectReleased += new NesGamepadModule.ButtonEventHandler(gamepad_SelectReleased);

            gamepad.StartPressed += new NesGamepadModule.ButtonEventHandler(gamepad_StartPressed);
            gamepad.StartReleased += new NesGamepadModule.ButtonEventHandler(gamepad_StartReleased);

            gamepad.DirectionChanged += new NesGamepadModule.DirectionEventHandler(gamepad_DirectionChanged);

            gamepad.EnableEvents(100);
        }

        void gamepad_DirectionChanged(NesGamepadModule sender, NesGamepad gamepad, bool up, bool down, bool left, bool right)
        {
            Debug.Print(gamepad + " Direction changed. Up:" + up + " Down:" + down + " Left:" + left +" Right:" + right);
        }

        void gamepad_StartReleased(NesGamepadModule sender, NesGamepad gamepad)
        {
            Debug.Print(gamepad + " Start Released");
        }

        void gamepad_StartPressed(NesGamepadModule sender, NesGamepad gamepad)
        {
            Debug.Print(gamepad + " Start Pressed");
        }

        void gamepad_SelectReleased(NesGamepadModule sender, NesGamepad gamepad)
        {
            Debug.Print(gamepad + " Select Released");
        }

        void gamepad_SelectPressed(NesGamepadModule sender, NesGamepad gamepad)
        {
            Debug.Print(gamepad + " Select Pressed");
        }

        void gamepad_ButtonBPressed(NesGamepadModule sender, NesGamepad gamepad)
        {
            Debug.Print(gamepad + " Button B Pressed");
        }

        void gamepad_ButtonAReleased(NesGamepadModule sender, NesGamepad gamepad)
        {
            Debug.Print(gamepad + " Button A Released");
        }

        void gamepad_ButtonBReleased(NesGamepadModule sender, NesGamepad gamepad)
        {
            Debug.Print(gamepad + " Button B Released");
        }

        void gamepad_ButtonAPressed(NesGamepadModule sender, NesGamepad gamepad)
        {
            Debug.Print(gamepad + " Button A Pressed");
        }











        private void PollingTest()
        {
            var timer = new GT.Timer(1000);

            timer.Tick += (t) =>
                {
                    gamepad.UpdateState();

                    ShowGamepadState(gamepad.GamepadAState, "A");
                    ShowGamepadState(gamepad.GamepadBState, "B");
                    Debug.Print(" -------- " + DateTime.Now.Ticks);
                };

            timer.Start();

        }

        private void ShowGamepadState(NesGamepadState state, string gamepadID)
        {
            if (state.IsButtonAPressed)
                Debug.Print(gamepadID + ": Button A Pressed");

            if (state.IsButtonBPressed)
                Debug.Print(gamepadID + ": Button B Pressed");

            if (state.IsStartPressed)
                Debug.Print(gamepadID + ": Start Pressed");

            if (state.IsSelectPressed)
                Debug.Print(gamepadID + ": Select Pressed");

            if (state.IsDPadLeftPressed)
                Debug.Print(gamepadID + ": Left Pressed");

            if (state.IsDPadRightPressed)
                Debug.Print(gamepadID + ": Right Pressed");

            if (state.IsDPadUpPressed)
                Debug.Print(gamepadID + ": Up Pressed");

            if (state.IsDPadDownPressed)
                Debug.Print(gamepadID + ": Down Pressed");
        
        }

    }
}
